package com.jingdun.sport.api.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jingdun.sport.bean.model.ApiResult;
import com.jingdun.sport.bean.model.Questions;
import com.jingdun.sport.bean.model.RoomAnswerRelation;
import com.jingdun.sport.bean.model.RoomQuestionRelation;
import com.jingdun.sport.service.QuestionsService;
import com.jingdun.sport.service.RoomAnswerRelationService;
import com.jingdun.sport.service.RoomQuestionRelationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/sport/questions/")
@Api(tags = "题目接口")
public class QuestionsController {

    @Autowired
    private QuestionsService questionService;

    @Autowired
    private RoomQuestionRelationService roomQuestionRelationService;

    /**
     * 新建题目接口
     */
    @PostMapping("/addQuestion")
    @ApiOperation(value = "新建题目接口", notes = "新建题目接口")
    public ResponseEntity<ApiResult> addQuestion(@RequestBody Questions question) {
        questionService.addQuestion(question);
        ApiResult apiResult = new ApiResult();
        //return ResponseEntity.ok().build();
        return ResponseEntity.ok(apiResult);
    }

    /**
     * 获取题目详情接口
     */
    @GetMapping("/getQuestionDetail")
    @ApiOperation(value = "获取题目详情接口", notes = "获取题目详情接口")
    public ApiResult getQuestionsDetail(long id) {
        Map m = new HashMap<String, Object>();
        m.put("id", id);
        Questions question = questionService.getQuestionDetail(m);
        //return question;
        ApiResult apiResult = new ApiResult();
        apiResult.setData(question);
        return apiResult;
    }

    /**
     * 删除题目接口
     */
    @PostMapping("/removeQuestion")
    @ApiOperation(value = "删除题目接口", notes = "删除题目接口")
    public ResponseEntity removeQuestions(long id) {
        Questions question = new Questions();
        question.setId(id);
        question.setEnabled("0");
        questionService.updateQuestion(question);
        ApiResult apiResult = new ApiResult();
        //return ResponseEntity.ok().build();
        return ResponseEntity.ok(apiResult);
    }

    /**
     * 修改题目接口
     */
    @PostMapping("/updateQuestion")
    @ApiOperation(value = "修改题目接口", notes = "修改题目接口")
    public ResponseEntity updateQuestions(long id, String content) {
        Questions question = new Questions();
        question.setId(id);
        question.setContent(content);
        questionService.updateQuestion(question);
        ApiResult apiResult = new ApiResult();
        //return ResponseEntity.ok().build();
        return ResponseEntity.ok(apiResult);
    }

    /**
     * 分页获取题目接口
     */
    @RequestMapping("/getPaginationQuestions")
    @ApiOperation(value = "分页获取题目接口", notes = "分页获取题目接口")
    public ResponseEntity<ApiResult> getPaginationQuestions(long size, long current) {
        Map m = new HashMap<String, Object>();
        m.put("size", size);
        m.put("current", current);
        Page<Map<String, Object>> page = questionService.getPaginationQuestions(m);
        ApiResult apiResult = new ApiResult();
        apiResult.setData(page);
        return ResponseEntity.ok(apiResult);
    }

    /**
     * 新建活动室问题关联关系接口
     */
    @PostMapping("/addRoomQuestionRelation")
    @ApiOperation(value = "新建活动室答案关联关系接口", notes = "新建活动室答案关联关系接口")
    public ResponseEntity<ApiResult> addRoomQuestionRelation(@RequestBody RoomQuestionRelation roomQuestionRelation) {
        roomQuestionRelationService.addRoomQuestionRelation(roomQuestionRelation);
        ApiResult apiResult = new ApiResult();
        return ResponseEntity.ok(apiResult);
    }

    /**
     * 获取活动室问题关联关系详情接口
     */
    @GetMapping("/getRoomQuestionRelationDetail")
    @ApiOperation(value = "获取活动室问题关联关系详情接口", notes = "获取活动室问题关联关系详情接口")
    public ApiResult getRoomQuestionRelationDetail(long roomId) {
        Map m = new HashMap<String, Object>();
        m.put("roomId", roomId);
        List<RoomQuestionRelation> roomQuestionRelation = roomQuestionRelationService.getRoomQuestionRelationDetail(m);
        ApiResult apiResult = new ApiResult();
        if (roomQuestionRelation != null && !roomQuestionRelation.isEmpty()) {
            apiResult.setData(roomQuestionRelation.get(0));
        }
        return apiResult;
    }

}